package com.zhou.framework.util;

import com.zhou.framework.exception.*;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author lang.zhou
 * @since 2023/2/18
 */
public class ExceptionUtil {
    /**
     * 获取异常堆栈信息
     * @param e 异常
     * @param mainInfo 提示信息
     */
    public static String getString(Throwable e,String mainInfo){
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter,true);
        e.printStackTrace(writer);
        return mainInfo + stringWriter.toString();
    }
    public static String getExceptionName(Throwable e){
        if(e == null){
            return "Success";
        } else if(e instanceof TokenException){
            return "Token";
        }  else if(e instanceof SessionOutTimeException){
            return "Session";
        } else if(e instanceof LoginException){
            return "Login";
        } else if(e instanceof CaptchaException){
            return "Captcha";
        } else if(e instanceof VerifyException){
            return "Verify";
        } else if(e instanceof RateLimiterException){
            return "RateLimiter";
        } else if(e instanceof LockException){
            return "Lock";
        } else if(e instanceof AuthException){
            return "Auth";
        } else if(e instanceof ForbiddenException){
            return "Forbidden";
        }
        return "SystemError";
    }
}
